package com.rt.easyjava.base.biz1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.base.biz1.bean.TmptTableBiz;
import com.rt.easyjava.base.biz1.bean.TmptTbColumnBiz;
import com.rt.easyjava.base.biz1.dto.TmptTableBizDto;
import com.rt.easyjava.base.biz1.dto.TmptTbColumnBizDto;
import com.rt.easyjava.base.biz1.mapper.TmptTableBizMapper;
import com.rt.easyjava.base.biz1.service.impl.base.TmptTableServiceImpl;
import com.rt.easyjava.base.biz1.service.intf.TmptTableBizService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.utils.others.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务实现类
 *
 * @author wuxiaoyong
 * @since 2020-07-04
 */
@Slf4j
@Service(value = "tmptTableService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TmptTableBizServiceImpl extends TmptTableServiceImpl implements TmptTableBizService {
    @Autowired
    private MyMapper mapper;
    @Resource
    private TmptTableBizMapper tmptTableBizMapper;
    @Resource
    private TmptTbColumnBizServiceImpl tmptTbColumnBizServiceImpl;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
     * create a object
     *
     * @param tmptTableBizDto
     * @return
     */
    @Override
    public Result<TmptTableBizDto> create(TmptTableBizDto tmptTableBizDto) {
        return super.create(tmptTableBizDto);
    }

    /**
     * update a object by Id
     *
     * @param tmptTableBizDto
     * @param updateWrapper
     * @return
     */
    @Override
    public Result<TmptTableBizDto> update(TmptTableBizDto tmptTableBizDto, UpdateWrapper updateWrapper) {
        return super.update(tmptTableBizDto, updateWrapper);
    }

    /**
     * 分页 (无合计功能)
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public Result<TmptTableBizDto> page(Page<TmptTableBiz> page, QueryWrapper<TmptTableBiz> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    /**
     * select all by queryWrapper
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public Result<TmptTableBizDto> selectBy(QueryWrapper<TmptTableBiz> queryWrapper) {
        return super.selectBy(queryWrapper);
    }

    /**
     * select one by id
     *
     * @param id
     * @return
     */
    @Override
    public Result<TmptTableBizDto> selectById(Long id) {
        return super.selectById(id);
    }


    /**
     * select many by idList
     *
     * @param idList
     * @return
     */
    @Override
    public Result selectBatchIds(List<Integer> idList) {
        List list = tmptTableBizMapper.selectBatchIds(idList);

        for (int i = 0; i < list.size(); i++) {

            TmptTableBiz tmptTableBiz = (TmptTableBiz) list.get(i);
            QueryWrapper<TmptTbColumnBiz> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tmpt_table_id", tmptTableBiz.getId());
            Result<TmptTbColumnBizDto> tmptTbColumnBizDtoResult = tmptTbColumnBizServiceImpl.mySelectBy(queryWrapper);
            List<TmptTbColumnBizDto> list1 = tmptTbColumnBizDtoResult.getList();
            List<TmptTbColumnBiz> list2 = mapper.maplist(list1, TmptTbColumnBiz.class);
            tmptTableBiz.setTmptTbColumnList(list2);

        }

        return Result.builder(list);
    }

    @Override
    public Result insertBankup(List<TmptTableBiz> tmptTableBizs) {
        for (TmptTableBiz tmptTableBiz : tmptTableBizs) {

            QueryWrapper<TmptTableBiz> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("keyrandom", tmptTableBiz.getKeyrandom());
            queryWrapper.eq("deleteflag", 0);
            List<TmptTableBiz> tmptTableBizs1 = tmptTableBizMapper.mySelectBy(queryWrapper);

            //如果数据库已经有模板就先删除
            if(!StringUtils.isNull(tmptTableBizs1)){
                for(int i=0;i<tmptTableBizs1.size();i++){
                    TmptTableBiz tmptTableBiz1 = tmptTableBizs1.get(i);

                    TmptTbColumnBizDto tmptTbColumnBizDto = new TmptTbColumnBizDto();
                    tmptTbColumnBizDto.setDeleteflag(1);
                    UpdateWrapper<TmptTbColumnBiz> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("tmpt_table_id", tmptTableBiz1.getId());
                    tmptTbColumnBizServiceImpl.update(tmptTbColumnBizDto,updateWrapper);

                    UpdateWrapper<TmptTableBiz> updateWrapper1 = new UpdateWrapper<>();
                    updateWrapper1.eq("id", tmptTableBiz1.getId());

                    tmptTableBiz1.setDeleteflag(1);
                    tmptTableBiz1.setKeyrandom(tmptTableBiz1.getKeyrandom()+"-del"+DateTool.getCurrentDateStr1("yyyyMMddHHmmssSSS"));
                    tmptTableBizMapper.update(tmptTableBiz1,updateWrapper1);
                }
            }



            int insert = tmptTableBizMapper.insert(tmptTableBiz);
            Integer id = tmptTableBiz.getId();
            List<TmptTbColumnBiz> tmptTbColumnBizList = tmptTableBiz.getTmptTbColumnList();
            List<TmptTbColumnBizDto> tmptTbColumnBizDtoListList = mapper.maplist(tmptTbColumnBizList, TmptTbColumnBizDto.class);
            for (TmptTbColumnBizDto tmptTbColumnBizDto : tmptTbColumnBizDtoListList) {
                tmptTbColumnBizDto.setTmptTableId(id);
                tmptTbColumnBizServiceImpl.create(tmptTbColumnBizDto);
            }

        }

        return Result.builder("success");
    }


    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
     * 分页查询：mybatis plus 自定义sql分页查询
     * 默认开启合计查询，按需注掉代码
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public Result<TmptTableBizDto> myPage(Page<TmptTableBiz> page, QueryWrapper<TmptTableBiz> queryWrapper) {
        return super.myPage(page, queryWrapper);
    }

    @Override
    public Result<TmptTableBizDto> mySelectBy(QueryWrapper<TmptTableBiz> queryWrapper) {
        return super.mySelectBy(queryWrapper);
    }

    @Override
    public Result<TmptTableBizDto> mySelectById(long id) {
        return super.mySelectById(id);
    }

}
